import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {$ajax, apiUrl} from "@/utils/ajax";

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/LoginView.vue')
    },
    {
        path: '/qrcode',
        name: 'qrcode',
        component: () => import('@/views/QrcodeView.vue')
    },
    {
        path: '/order_list',
        name: 'orderLst',
        component: () => import('@/views/OrderView.vue')
    },
    {
        path: '/modify',
        name: 'modify',
        component: () => import('@/views/ModifyView.vue')
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    document.title = "智行九州渠道管理系统"
    if (to.name !== 'login') {
        //验证登录信息
        $ajax.post(`${apiUrl}authorization/checkMerchantLogin`).then(({data:{result}}) => {
            window.localStorage.setItem("merchant_account_login", JSON.stringify(result))
            next()
        }).catch(() => {
            next({name: 'login'})
        })
    } else {
        next()
    }
})


export default router
